package com.zhilianbao.leyaogo.ui.adapter.home.notice;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bql.utils.CheckUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.app.LeYaoGoApplication;
import com.zhilianbao.leyaogo.model.response.home.BaseDataBean;
import com.zhilianbao.leyaogo.ui.activity.me.moneyaccount.BalanceActivity;
import com.zhilianbao.leyaogo.utils.LogicCodeBlock;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.marqueeview.MarqueeView;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 首页每日一鲜 ItemViewProvider
 */
public class HomeAppNoticeViewProvider
        extends ItemViewProvider<HomeAppNotice, HomeAppNoticeViewProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_day_refresh, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(
            @NonNull ViewHolder holder, @NonNull HomeAppNotice homeAppNotice) {
        holder.mTvNoticeTitle.setText(CheckUtils.isEmpty(homeAppNotice.getData().getTitle()) ? LeYaoGoApplication.getContext().getString(R.string.day_refresh_default) : homeAppNotice.getData().getTitle());
        List<String> noticeList = new ArrayList<>();
        for (BaseDataBean baseDataBean : homeAppNotice.getData().getList()) {
            noticeList.add(baseDataBean.getName());
        }
        holder.mMarqueeView.stopFlipping();
        holder.mMarqueeView.startWithList(noticeList);
        holder.mMarqueeView.setOnItemClickListener((position, textView) -> {
            BaseDataBean baseDataBean = homeAppNotice.getData().getList().get(position);
            switch (baseDataBean.getType()) {
                //跳转到商品详情
                case 0:
                    Utils.startGoodsDetails((Activity) holder.mMarqueeView.getContext(), (int) baseDataBean.getId(), (int)baseDataBean.getGoodsSkuId(), false);
                    break;
                //跳转到商品列表
                case 1:
                    Utils.startGoodsClassifyList((Activity) holder.mMarqueeView.getContext(), baseDataBean.getId(), baseDataBean.getName());
                    break;
                //跳转到专题活动详情
                case 2:
                case 3:
                    Utils.startActivityDetails((Activity) holder.mMarqueeView.getContext(), baseDataBean.getId(), false);
                    break;
                //充值有礼
                case 4:
                    if (Utils.isUserLogin(true, LogicCodeBlock.LogicState.Recharge.value)) {
                        Utils.startActivity((Activity) holder.mMarqueeView.getContext(), BalanceActivity.class);
                    } else {
                        LogicCodeBlock.create().registerLogicCodeCallback(logicState -> {
                                    if (logicState == LogicCodeBlock.LogicState.Recharge.value)
                                        Utils.startActivity((Activity) holder.mMarqueeView.getContext(), BalanceActivity.class);
                                }
                        );
                    }

                    break;
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //轮播广告
        private MarqueeView mMarqueeView;

        private TextView mTvNoticeTitle;

        ViewHolder(View itemView) {
            super(itemView);
            mMarqueeView = (MarqueeView) itemView.findViewById(R.id.marquee_view);
            mTvNoticeTitle = (TextView) itemView.findViewById(R.id.tv_notice_title);
        }
    }
}